$.fn.extend({
    initTable: function (options) {
        var el = $(this);
        var defaultConfig = {
            method: 'get',
            cache: false,
            clickToSelect: true,
            striped: true,     //使表格带有条纹
            idField: "id",  //标识哪个字段为id主键
            uniqueId: "id",
            cusRefresh: $("#refresh"),
            conditionSearch: $("#conditionSearch"),
            extraParams: {},
            queryParams: function (params) {
                var pageSize = params.limit;
                var offset = params.offset;
                var curPage = (offset / pageSize) + 1;
                var params = {
                    curPage: curPage
                };
                return params;
            }, //参数
            silent: true,  //刷新事件必须设置
            formatLoadingMessage: function () {
                return "请稍等，正在加载中...";
            },
            formatNoMatches: function () {  //没有匹配的结果
                return '无符合条件的记录';
            },
            onLoadError: function () {
                el.bootstrapTable('removeAll');
            },
            onLoadSuccess: function (data) {
                // 绑定按钮事件
                // if (options.buttons) {
                //     var buttons = options.buttons;
                //     buttons.forEach(function (button, index, arr) {
                //         $("." + button.id).click(function (event) {
                //             button.handler(el, event);
                //         });
                //     });
                // }
            },
            onClickCell: function (field, value, row, el) {
                var img = el.find("img[class='preview']");
                if (img.length != 0) {
                    var curImgUrl = img.attr("value");
                    $.photos("preview", curImgUrl);
                }
            }

        };
        var _config = $.extend(true, defaultConfig, options);

        if (!_config.url) {
            return;
        }

        if (!_config.columns) {
            return;
        }

        // 综合条件查询按钮事件
        _config.conditionSearch.unbind("click").on("click", function () {
            el.bootstrapTable("refresh");
        });

        // 绑定刷新按钮点击事件
        _config.cusRefresh.unbind("click").click(function () {
            el.bootstrapTable("refresh");
        });

        if (_config.buttons) {
            var buttons = _config.buttons;
            var width = 0;
            buttons.forEach(function (button) {
                width += (30 + (button.name.length + 1) * 15);
            });
            var _obj = {
                title: '操作', align: 'left', valign: 'middle', width: width, formatter: function (value, row, index) {
                    var _result = "";
                    buttons.forEach(function (button) {
                        // 默认为false ==> 显示
                        var hidden = false;
                        if (button.hidden != null) {
                            if (typeof (eval(button.hidden)) == "function") {
                                hidden = button.hidden(row);
                            }
                        }
                        if (!hidden) {
                            button.btnClass += (" " + button.id);
                            var _tmp = "<a class='" + button.btnClass + "' dataId='" + row.id + "'><i class='" + button.icon + "'></i>" + button.name + "</a> ";
                            _result += _tmp;
                        }
                        // 绑定事件
                        $(document).on("click", "." + button.id, function (event) {
                            button.handler(row, event);
                        });
                    });
                    return _result;
                }
            };
            _config.columns.push(_obj);
        }

        // 是否分页
        if (_config.isPagination) {
            var paginationConfig = {
                sidePagination: "server",//表格分页的位置
                pagination: true,    //在表格底部显示分页工具栏
                pageSize: 15,
                pageNumber: 1,
                pageList: [], // 隐藏可选分页
                formatShowingRows: function (pageFrom, pageTo, totalRows) {
                    return "第 " + ((pageFrom - 1) / 15 + 1) + " 页 每页 15 条 - 共 " + totalRows + " 条数据 ";
                },
                responseHandler: function (res) {
                    var obj = new Object();
                    obj["rows"] = res == null ? [] : res.content;
                    obj["total"] = res == null ? 0 : res.page.totalRows;
                    return obj;
                }
            };
            $.extend(_config, paginationConfig);
        }

        // 添加额外的请求参数
        _config.queryParams = function (param) {
            var pageSize = param.limit;
            var offset = param.offset;
            var curPage = (offset / pageSize) + 1;
            var params = new Object();
            params["curPage"] = curPage;
            if (_config.extraParams) {
                $.each(_config.extraParams, function (name, value) {
                    params[name] = value;
                });
            }
            if (_config.extraParamsDom) {
                $.each(_config.extraParamsDom, function (name, item) {
                    params[name] = item.val();
                });
            }
            return params;
        };

        $.include(webRoot + '/resources/console/extra/bootstrap-table/css/', ["bootstrap-table.min.css"]);
        $.getScript(webRoot + '/resources/console/extra/bootstrap-table/bootstrap-table.min.js').done(function () {
            el.bootstrapTable(_config);
        });
    }
});